// pages/register/register.js
const app = getApp()
var code = require('../../utils/com.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    telephone: '',
    telephoneCode: '',
    sec: "60",
    isShow: false,
    isRegister: false,
    isRePhone: '',
    isReWbId: []
  },
  /**
   * 获取验证码方法,验证手机号
   */
  getPhoneCode: function(e) {
    var that = this;
    var phone = that.data.telephone;
    if (phone == '') {
      wx.showToast({
        title: '手机号不能为空',
      })
      return false
    } else if (phone.length != 11) {
      wx.showToast({
        title: '手机号长度有误！',
        duration: 1500
      })
      return false;
    }

    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(phone)) {
      wx.showToast({
        title: '手机号有误！',
        duration: 1500
      })
      return false;
    }
    var _this = this;　　　　 //防止this对象的混杂，用一个变量来保存
    // 这里添加调用发送短信接口
    wx.request({
      url: app.globalData.apiBaseURL + '/getSMSVCode',
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        userId: app.globalData.databaseUser.userId,
        phoneNum: this.data.telephone
      },
      success: function(data) {
        if (data.data.status == 2) {
          wx.showToast({
            title: data.data.msg,
            icon: 'none'
          })
        } else {
          var time = _this.data.sec　　 //获取最初的秒数
          code.getCode(_this, time);　　 //调用倒计时函数
          wx.showToast({
            title: data.data.msg,
            icon: 'success',
            duration: 1500
          })
        }
      }
    })
  },
  goToAddWb: function(e) {
    wx.navigateTo({
      url: '../addWbId/addwbId',
    })
  },
  telephoneInput: function(e) {
    this.setData({
      telephone: e.detail.value
    })
  },
  telephoneCodeInput: function(e) {
    this.setData({
      telephoneCode: e.detail.value
    })
  },
  getTips: function() {
    wx.navigateTo({
      url: '../guide/guide',
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  toTask: function() {
    wx.navigateTo({
      url: '../task/task',
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  showTopTips: function() {
    var that = this
    let query = {
      userId: app.globalData.databaseUser.userId,
      openId: app.globalData.databaseUser.userAppId,
      phoneNum: this.data.telephone,

      smsVcode: this.data.telephoneCode
    }
    console.log(query)
    if (query.phoneNum == '') {
      wx.showToast({
        title: '手机号不得为空',
        icon: 'none'
      })
    } else if (query.smsVcode == '') {
      wx.showToast({
        title: '验证码不得为空',
        icon: 'none'
      })
    } else {
      var _this = this
      wx.request({
        url: app.globalData.apiBaseURL + '/user/saveUserInfo',
        method: 'post',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: query,
        success: function(data) {
          if (data.data.status == 1) {
            wx.showToast({
              title: data.data.msg,
              icon: 'success'
            })
            _this.clearInput()
            _this.setData({
              isRegister: true,
              isRePhone: data.data.user.userPhoneNum,
              isReWbId: data.data.weiboIds
            })
          } else {
            wx.showToast({
              title: data.data.msg,
              icon: 'none',
              duration: 1500
            })
          }
        }
      })
    }

  },
  clearInput: function() {
    this.setData({
      telephone: '',
      telephoneCode: ''
    })
  },
  toLogin: function() {
    wx.navigateTo({
      url: '../login/login',
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    console.log(app.globalData.databaseUser.userPhoneNum)
    console.log(app.globalData.weiboIds)
    if (app.globalData.weiboIds) {
      this.setData({
        isReWbId: app.globalData.weiboIds
      })
    }
    if (app.globalData.databaseUser.userPhoneNum) {
      this.setData({
        isRegister: true,
        isRePhone: app.globalData.databaseUser.userPhoneNum
      })
      console.log('注册过手机')
    } else {
      console.log('没注册')
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.onLoad()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})